package com.edu.dao.impl;

import com.edu.dao.MyBatisUtil;
import com.edu.dao.UserDao;
import com.edu.dao.UserMapper;
import com.edu.data.User;
import com.edu.util.Validator;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {

	SqlSession sqlSession= MyBatisUtil.createSession();
	UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

	@Override
	public User findById(String id) throws SQLException {
		User user=userMapper.findById(id);
		return user;
	}

//	//由结果集生成对象
//	private User createUserByResultSet(ResultSet rs) throws SQLException {
//		User user = new User();
//		user.setUserId(rs.getString("eu_user_id"));
//		user.setUserName(rs.getString("eu_user_name"));
//		user.setPassword(rs.getString("eu_password"));
//		user.setMale(rs.getString("eu_sex").equals("T"));
//		user.setBirthday(rs.getDate("eu_birthday"));
//		user.setIdentityCode(rs.getString("eu_identity_code"));
//		user.setEmail(rs.getString("eu_email"));
//		user.setMobile(rs.getString("eu_mobile"));
//		user.setAddress(rs.getString("eu_address"));
//		user.setStatus(rs.getInt("eu_status"));
//		user.setLogin(rs.getBoolean("eu_login"));
//		return user;
//	}
//
	public void save(User user) throws SQLException {//新增用户信息
		userMapper.save(user);
	}

	//更新用户信息
	public void update(User user) throws SQLException {
		userMapper.update(user);
	}

	//根据ID删除用户
	public void delete(String userId) throws SQLException {
		userMapper.delete(userId);
	}

	//查询用户记录条数
//	public int getUserRowCount() {
//		return userMapper.getUserRowCount();
//	}
//
//	//获取本页要显示的用户
//	public List<User> getUsers(Pager pager) throws SQLException {
//		List<User> rtn = new ArrayList<User>();
//
//		String sql = "select * from  easybuy_user  ";
//		if (pager != null)//分页显示
//			sql = this.getSqlForPages("easybuy_user", "EU_USER_ID","", pager);
//		PreparedStatement prepareStatement = connection.prepareStatement(sql);
//		ResultSet rs = prepareStatement.executeQuery();
//		while (rs.next()) {
//			rtn.add(createUserByResultSet(rs));
//		}
//		rs.close();
//		prepareStatement.close();
//		return rtn;
//	}
//
//	@Override
//	public void addAdress(User user, String address) {//为用户添加收货地址
//		String sql = " update EASYBUY_USER  set EU_ADDRESS=EU_ADDRESS+? where EU_USER_ID=?";
//		PreparedStatement ps;
//		try {
//			ps = connection.prepareStatement(sql);
//			address=";"+address;
//			ps.setString(1, address);
//			ps.setString(2, user.getUserId());
//			ps.executeUpdate();
//			ps.close();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//	}
//
	@Override
	public void setLogin(User user, boolean isLogin) {//设置用户登录状态
		userMapper.setLogin(user,isLogin);
	}

	@Override
	public List<User> getUsers() {
		return userMapper.getUsers();
	}


}
